public class UIManager : Gtk.UIManager 
{
  private static UIManager  instance = null;
  private Gtk.ActionGroup   action_group = new Gtk.ActionGroup ("main_action_group");	
  
  construct 
  {
   	try
        {
          this.add_ui_from_file ("ui.xml");
          this.insert_action_group (action_group, 0);
        
          /*Tworzye akcje*/
          /*Menu "Plik"*/
          action_group.add_action_with_accel (
            new Gtk.Action ("file", "_Plik", null, null), null 
          );
          action_group.add_action_with_accel (
            new Gtk.Action ("file_new", "Nowy", null, Gtk.STOCK_NEW), "<ctrl>n" 
          );
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_open", "Otwórz", null, Gtk.STOCK_OPEN), "<ctrl>o"
          );
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_last_open", "Ostatnio otwierane", null, null), null  
          );          
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_save", "Zapisz", null, Gtk.STOCK_SAVE), "<ctrl>s"  
          );          
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_save_as", "Zapisz jako...", null, Gtk.STOCK_SAVE_AS), null  
          );
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_save_all", "Zapisz wszystkie", null, null), "<shift><ctrl>s"  
          );
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_close", "Zamknij", null, Gtk.STOCK_CLOSE), "<ctrl>w"  
          );                              
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_close_all", "Zamknij wszystkie", null, null), "<shift><ctrl>w"  
          );
          action_group.add_action_with_accel (
  	    new Gtk.Action ("file_quit", "Zakończ", null, Gtk.STOCK_QUIT), "<alt>F4"  
          );
          
          //Menu "edycja"
          action_group.add_action_with_accel (
            new Gtk.Action ("edit", "_Edycja", null, null), null 
          );
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_undo", "Cofnij", null, Gtk.STOCK_UNDO), "<ctrl>z" 
          );                    
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_redo", "Ponów", null, Gtk.STOCK_REDO), "<shift><ctrl>z" 
          );
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_cut", "Wytnij", null, Gtk.STOCK_CUT), null 
          );        
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_copy", "Skopiuj", null, Gtk.STOCK_COPY), null 
          );
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_paste", "Wklej", null, Gtk.STOCK_PASTE), null 
          );          
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_delete", "Usuń", null, Gtk.STOCK_DELETE), null 
          );          
          action_group.add_action_with_accel (
            new Gtk.Action ("edit_select_all", "Zaznacz wszytsko", null, Gtk.STOCK_SELECT_ALL), "<ctrl>a" 
          );
          
          //Menu "wyszukaj"
          action_group.add_action_with_accel (
          	new Gtk.Action ("search", "Wy_szukiwanie", null, null), null
          );
          action_group.add_action_with_accel (
          	new Gtk.Action ("search_find", "Znajdź", null, Gtk.STOCK_FIND), "<ctrl>f"
          );
          this.get_action ("search_find").set_sensitive (false);     
          action_group.add_action_with_accel (
          	new Gtk.Action ("search_find_next", "Znajdź następny", null, null), "<ctrl>g"
          );
          this.get_action ("search_find_next").set_sensitive (false);
          action_group.add_action_with_accel (
          	new Gtk.Action ("search_find_prev", "Znajdź poprzedni", null, null), "<ctrl><shift>g"
          );
          this.get_action ("search_find_prev").set_sensitive (false);
          action_group.add_action_with_accel (
          	new Gtk.Action ("search_replace", "Zamień", null, Gtk.STOCK_FIND_AND_REPLACE), "<ctrl>h"
          );
          this.get_action ("search_replace").set_sensitive (false);          
          action_group.add_action_with_accel (
          	new Gtk.Action ("search_goto_line", "Przejdź do wiersza", null, Gtk.STOCK_JUMP_TO), "<ctrl>i"
          );          
          this.get_action ("search_goto_line").set_sensitive (false);
          
          this.create_last_open_menu ();                                             
        }
        catch (GLib.Error err)
        {
            /*Brak pliku z definicją UI*/
			var msg = new Gtk.MessageDialog (
                		null, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, 
		                "Failed to load UI\n" + err.message
		        );
            msg.run ();
            msg.destroy ();        
        }  
  }
  
  private UIManager ()
  {
  }
 
  private inline void create_last_open_menu ()
  {
  	weak Gtk.Widget last_open_menu = this.get_widget ("/main_menu/file/file_last_open");
	if (last_open_menu != null)
	{	
		Gtk.RecentFilter text_filter = new Gtk.RecentFilter ();
		text_filter.add_mime_type ("text/*");		
		
		Gtk.RecentChooserMenu recent_menu = new Gtk.RecentChooserMenu ();
		recent_menu.add_filter (text_filter);	
		recent_menu.set_sort_type (Gtk.RecentSortType.MRU);	
		recent_menu.set_show_not_found (false);
		recent_menu.item_activated += (recent) => {
			DocumentManager.get_instance().create (
				GLib.Filename.from_uri(recent.get_current_item().get_uri ())
			);
		};
		
		((Gtk.MenuItem) last_open_menu).set_submenu (recent_menu); 
	}
  }	 	
  
  public static UIManager get_instance ()
  {
  	if (instance == null)
  	{
  		instance = new UIManager ();
	}
	return instance;
  }
  
  public weak Gtk.Action get_action (string name)
  {
    return this.action_group.get_action (name);
  }
  
  public void set_sensitive_doc_actions (bool sensitive = true)
  {
  	string[] doc_actions = {
  		//Menu "plik"
  		"file_save",
  		"file_save_as",
  		"file_save_all",
  		"file_close",
  		"file_close_all",
  		//Menu "Edycja"
  		"edit_undo",
  		"edit_redo",
  		"edit_cut",
  		"edit_copy",
  		"edit_paste",
  		"edit_delete",
  		"edit_select_all"
  		//Menu "Wyszukaj"
//  		"search_find",  		
//  		"search_replace",
//  		"search_goto_line"
  	};
  	
  	foreach (string doc_action in doc_actions)
  	{
  		this.get_action(doc_action).set_sensitive (sensitive);
  	}	 
  }
}
